/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import collection.Products;
import dao.ProductDAO;
import dto.Product;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.XMLUtil;

/**
 *
 * @author duonghung1369
 */
public class ProductController extends BaseController {

    private Product product;
    private List<Product> products;
    private ProductDAO productDAO;
    private int category_id;
    private int id;
    private Log log = LogFactory.getLog(ProductController.class);
    private InputStream productXMLStream;

    public ProductController() {
        super();
        productDAO = new ProductDAO(Product.class);
    }

    /**
     * Using JAXB to load products in to index page.
     * Just show products that deleted = false, isShowed = true.
     * @return
     */
    @Override
    public String execute() {
        try {
            // load category left side bar
            loadSideBar();
            JAXBContext context = JAXBContext.newInstance(Products.class);
            String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "products.xml";
            Products objProducts = (Products) XMLUtil.unmarshalObject(context, filePath);
            int categoryId = -1;
            objProducts.filterDisplayHomepage(categoryId);
            products = objProducts.getProducts();
            return SUCCESS;
        } catch (Exception ex) {
            log.error("ProductController.execute()", ex);
            return ERROR;
        }
    }

    /**
     * Load products that belong to clicked category and deleted = false.
     * Using JAXB to unmarshal products.xml and process.
     * @return
     */
    public String list() {
        try {
            // load category left side bar
            loadSideBar();

//            CategoryDAO cateDAO = new CategoryDAO(Category.class);
//            cateDAO.openSession();
//            Category cate = cateDAO.get(category_id);
//            products = cate.getProducts();
//            Hibernate.initialize(cate.getProducts());
//            cateDAO.closeSession();

            JAXBContext context = JAXBContext.newInstance(Products.class);
            String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "products.xml";
            Products objProducts = (Products) XMLUtil.unmarshalObject(context, filePath);
            objProducts.filterDisplayHomepage(category_id);
            products = objProducts.getProducts();
        } catch (Exception ex) {
            log.error("ProductController.list()", ex);
        }
        return SUCCESS;
    }

    /**
     * Show detail of product.
     * Using JAXB to unmarshal products.xml to object and get product that not deleted.
     *
     * @return if product = null, return ERROR with addActionMessage
     */
    public String detail() {
        try {
            // load category left side bar
            loadSideBar();

//            productDAO.openSession();
//            product = productDAO.get(id);
//            Hibernate.initialize(product.getImages());
//            productDAO.closeSession();

            JAXBContext context = JAXBContext.newInstance(Products.class);
            String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "products.xml";
            Products objProducts = (Products) XMLUtil.unmarshalObject(context, filePath);
            product = objProducts.contains(id);

            // check if product deleted of user input which not available product id
            if (product == null || product.isIsDeleted()) {
                addActionMessage("This product temporary not available!");
                return ERROR;
            }

        } catch (Exception ex) {
            log.error("ProductController.product_detail()", ex);
        }
        return SUCCESS;
    }

    /**
     * Response an input stream of products.xml for client
     * @return DOM tree of products.xml to suggest user search product by name.
     * @throws JAXBException
     * @throws FileNotFoundException
     */
    public String searchProductXMLCallback() throws JAXBException, FileNotFoundException {
        String filePath = System.getProperty("rootPath") + util.StringUtil.XML_FOLDER_PATH + "products.xml";
        File f = new File(filePath);        
        productXMLStream = new FileInputStream(f);
        System.out.println("init inputStream successfull!");
        return SUCCESS;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @return the products
     */
    public List<Product> getProducts() {
        return products;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<Product> products) {
        this.products = products;
    }

    /**
     * @return the category_id
     */
    public int getCategory_id() {
        return category_id;
    }

    /**
     * @param category_id the category_id to set
     */
    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the productXMLStream
     */
    public InputStream getProductXMLStream() {
        return productXMLStream;
    }

    /**
     * @param productXMLStream the productXMLStream to set
     */
    public void setProductXMLStream(InputStream productXMLStream) {
        this.productXMLStream = productXMLStream;
    }
}
